﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Xajhzwb.Components.Publish
{
    public class PublishManage
    {
        private List<PublishTask> m_ListPublish;
        public List<PublishTask> ListPublish
        {
            get { return m_ListPublish; }
        }

        public PublishManage()
        {
            m_ListPublish = new List<PublishTask>();
        }

        ~PublishManage()
        {
        }

        public void AddPublishTask(PublishTask pt)
        {
            ListPublish.Add(pt);
            TaskInit(pt);
            pt.startPublic();
        }

        public void AddSaveTempDataTask(PublishTask pt)
        {
            ListPublish.Add(pt);
            TaskTempSaveInit(pt);
            pt.startSaveTempData();
        }

        private void TaskTempSaveInit(PublishTask publishTask)
        {
            publishTask.PublishTempDataCompleted += this.onPublishTempDataCompleted;
        }

        private void TaskInit(PublishTask publishTask)
        {
            publishTask.PublishCompleted  += this.onPublishCompleted;
            publishTask.PublishFailed  += this.onPublishFailed;
            publishTask.PublishStarted  += this.onPublishStarted;
            publishTask.PublishError  += this.onPublishError;
            publishTask.PublishTempDataCompleted += this.onPublishTempDataCompleted;
            publishTask.PublishLog += this.onPublishLog;
            //pTask.RunTask += this.onRunQidorTask;
        }

        private void onPublishLog(object sender, PublishLogEventArgs e)
        {
            if (e_PublishLog != null && !e.Cancel)
            {
                e_PublishLog(sender, e);
            }
        }

        private void onPublishCompleted(object sender, PublishCompletedEventArgs e)
        {
            PublishTask pt = (PublishTask)sender;
            m_ListPublish.Remove(pt);
            pt = null;

            if (e_PublishCompleted != null && !e.Cancel)
            {
                e_PublishCompleted(sender, e);
            }

        }

        private void onPublishFailed(object sender, PublishFailedEventArgs e)
        {
            PublishTask pt = (PublishTask)sender;
            m_ListPublish.Remove(pt);
            pt = null;

            if (e_PublishFailed != null && !e.Cancel)
            {
                e_PublishFailed(sender, e);
            }

        }

        private void onPublishStarted(object sender, PublishStartedEventArgs e)
        {
            if (e_PublishStarted != null && !e.Cancel)
            {
                e_PublishStarted(sender, e);
            }
        }

        private void onPublishError(object sender, PublishErrorEventArgs e)
        { 
            PublishTask pt = (PublishTask)sender;
            m_ListPublish.Remove(pt);
            pt = null;

            if (e_PublishError != null && !e.Cancel)
            {
                e_PublishError(sender, e);
            }

        }

        private void onPublishTempDataCompleted(object sender, PublishTempDataCompletedEventArgs e)
        {

            PublishTask pt = (PublishTask)sender;
            m_ListPublish.Remove(pt);
            pt = null;

            if (e_PublishTempDataCompleted != null && !e.Cancel)
            {
                e_PublishTempDataCompleted(sender, e);
            }
        }

        //private void onRunQidorTask(object sender, cRunTaskEventArgs e)
        //{
        //    e_RunTask(this, new RunTaskEventArgs(e.MessType, e.RunName, e.RunPara));
        //}

        #region 事件

        private event EventHandler<PublishCompletedEventArgs> e_PublishCompleted;
        public event EventHandler<PublishCompletedEventArgs> PublishCompleted
        {
            add { e_PublishCompleted += value; }
            remove { e_PublishCompleted -= value; }
        }

        private event EventHandler<PublishFailedEventArgs> e_PublishFailed;
        public event EventHandler<PublishFailedEventArgs> PublishFailed
        {
            add { e_PublishFailed += value; }
            remove { e_PublishFailed -= value; }
        }

        private event EventHandler<PublishStartedEventArgs> e_PublishStarted;
        public event EventHandler<PublishStartedEventArgs> PublishStarted
        {
            add { e_PublishStarted += value; }
            remove { e_PublishStarted -= value; }
        }

        private event EventHandler<PublishErrorEventArgs> e_PublishError;
        public event EventHandler<PublishErrorEventArgs> PublishError
        {
            add { e_PublishError += value; }
            remove { e_PublishError -= value; }
        }

        private event EventHandler<PublishTempDataCompletedEventArgs> e_PublishTempDataCompleted;
        public event EventHandler<PublishTempDataCompletedEventArgs> PublishTempDataCompleted
        {
            add { e_PublishTempDataCompleted += value; }
            remove { e_PublishTempDataCompleted -= value; }
        }

        private event EventHandler<PublishLogEventArgs> e_PublishLog;
        public event EventHandler<PublishLogEventArgs> PublishLog
        {
            add { e_PublishLog += value; }
            remove { e_PublishLog -= value; }
        }

        //private event EventHandler<cRunTaskEventArgs> e_RunTask;
        //internal event EventHandler<cRunTaskEventArgs> RunTask
        //{
        //    add { e_RunTask += value;  }
        //    remove { e_RunTask -= value;  }
        //}

        #endregion
    }
}
